#!/bin/bash

# 检查是否提供了模块名
if [ -z "$1" ]; then
    echo "请提供模块名称"
    echo "用法: ./create-module.sh ModuleName"
    exit 1
fi

# 将首字母大写
MODULE_NAME=$(echo $1 | sed 's/./\U&/g')
MODULE_NAME_LOWER=$(echo $1 | sed 's/./\L&/g')

echo "正在创建模块: $MODULE_NAME..."

# 检查模块是否已存在
if [ -d "Modules/$MODULE_NAME" ]; then
    echo "错误: 模块 $MODULE_NAME 已经存在"
    exit 1
fi

# 创建模块目录结构
mkdir -p Modules/$MODULE_NAME/app/Http/Controllers
mkdir -p Modules/$MODULE_NAME/app/Providers
mkdir -p Modules/$MODULE_NAME/resources/views
mkdir -p Modules/$MODULE_NAME/resources/views/components/layouts
mkdir -p Modules/$MODULE_NAME/routes
mkdir -p Modules/$MODULE_NAME/config
mkdir -p Modules/$MODULE_NAME/database/migrations
mkdir -p Modules/$MODULE_NAME/database/seeders

# 创建模块配置文件
echo "<?php

return [
    'name' => '$MODULE_NAME',
];" > Modules/$MODULE_NAME/config/config.php

# 创建路由文件
echo "<?php

use Illuminate\Support\Facades\Route;
use Modules\\\\$MODULE_NAME\\\\Http\\\\Controllers\\\\${MODULE_NAME}Controller;

// 为$MODULE_NAME模块定义传统路由
Route::middleware('web')->group(function () {
    // $MODULE_NAME首页路由
    Route::get('/$MODULE_NAME_LOWER', [${MODULE_NAME}Controller::class, 'index'])->name('$MODULE_NAME_LOWER.index');
});" > Modules/$MODULE_NAME/routes/web.php

# 创建控制器
echo "<?php

namespace Modules\\\\$MODULE_NAME\\\\Http\\\\Controllers;

use App\\\\Http\\\\Controllers\\\\Controller;
use Illuminate\\\\Http\\\\Request;

class ${MODULE_NAME}Controller extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('$MODULE_NAME_LOWER::index');
    }
}" > Modules/$MODULE_NAME/app/Http/Controllers/${MODULE_NAME}Controller.php

# 创建视图文件
echo "<x-$MODULE_NAME_LOWER::layouts.master>
    <div class=\"container mx-auto px-4 py-8\">
        <div class=\"bg-white rounded-lg shadow-lg p-6\">
            <h1 class=\"text-2xl font-bold text-gray-800 mb-4\">$MODULE_NAME 模块</h1>
            
            <p class=\"text-gray-600 mb-6\">欢迎使用 {!! config('$MODULE_NAME_LOWER.name', '$MODULE_NAME') !!} 模块</p>
            
            <div class=\"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6\">
                <div class=\"bg-blue-50 p-4 rounded-lg border border-blue-200\">
                    <h2 class=\"text-lg font-semibold text-blue-700\">功能一</h2>
                    <p class=\"text-sm text-blue-600 mt-2\">这是功能一的描述</p>
                </div>
                
                <div class=\"bg-green-50 p-4 rounded-lg border border-green-200\">
                    <h2 class=\"text-lg font-semibold text-green-700\">功能二</h2>
                    <p class=\"text-sm text-green-600 mt-2\">这是功能二的描述</p>
                </div>
                
                <div class=\"bg-purple-50 p-4 rounded-lg border border-purple-200\">
                    <h2 class=\"text-lg font-semibold text-purple-700\">功能三</h2>
                    <p class=\"text-sm text-purple-600 mt-2\">这是功能三的描述</p>
                </div>
            </div>
        </div>
    </div>
</x-$MODULE_NAME_LOWER::layouts.master>" > Modules/$MODULE_NAME/resources/views/index.blade.php

# 创建布局文件
echo "<!DOCTYPE html>
<html lang=\"{{ str_replace('_', '-', app()->getLocale()) }}\">
    <head>
        <meta charset=\"utf-8\">
        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
        <meta name=\"csrf-token\" content=\"{{ csrf_token() }}\">
        <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">

        <title>$MODULE_NAME Module - {{ config('app.name', 'Laravel') }}</title>

        <meta name=\"description\" content=\"{{ \$description ?? '$MODULE_NAME模块' }}\">
        <meta name=\"keywords\" content=\"{{ \$keywords ?? 'laravel,$MODULE_NAME_LOWER,module' }}\">
        <meta name=\"author\" content=\"{{ \$author ?? 'Laravel Admin' }}\">

        <!-- Fonts -->
        <link rel=\"preconnect\" href=\"https://fonts.bunny.net\">
        <link href=\"https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap\" rel=\"stylesheet\" />

        <!-- Tailwind CSS -->
        <script src=\"https://cdn.tailwindcss.com\"></script>

        <style>
            [x-cloak] { display: none !important; }
        </style>
    </head>

    <body class=\"font-sans antialiased bg-gray-100\">
        <div class=\"min-h-screen\">
            <nav class=\"bg-white border-b border-gray-200\">
                <div class=\"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8\">
                    <div class=\"flex justify-between h-16\">
                        <div class=\"flex\">
                            <div class=\"flex-shrink-0 flex items-center\">
                                <span class=\"text-xl font-bold text-gray-800\">$MODULE_NAME Module</span>
                            </div>
                        </div>
                    </div>
                </div>
            </nav>

            <main>
                {{ \$slot }}
            </main>
        </div>
    </body>
</html>" > Modules/$MODULE_NAME/resources/views/components/layouts/master.blade.php

# 创建模块.json文件
echo "{
    \"name\": \"$MODULE_NAME\",
    \"alias\": \"$MODULE_NAME_LOWER\",
    \"description\": \"\",
    \"keywords\": [],
    \"priority\": 0,
    \"providers\": [
        \"Modules\\\\$MODULE_NAME\\\\Providers\\\\${MODULE_NAME}ServiceProvider\"
    ],
    \"files\": []
}" > Modules/$MODULE_NAME/module.json

# 更新模块状态文件
echo "{
    \"Admin\": true,
    \"$MODULE_NAME\": true
}" > modules_statuses.json

echo "模块 $MODULE_NAME 创建成功！"
echo "现在你可以访问 /$MODULE_NAME_LOWER 来查看你的新模块。" 